package com.harmontronics.hanma.paas.paas.domain.iam.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.valueobject.HmRoleMenuFields;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.persistence.HmRoleMenuRepository;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmRoleMenu;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.harmontronics.hanma.paas.paas.common.PageBase;

import com.harmontronics.hanma.paas.paas.domain.iam.repository.mapper.HmRoleMenuMapper;
import com.harmontronics.hanma.paas.paas.domain.iam.repository.po.HmRoleMenuPO;
import java.util.List;

@Service("hmRoleMenuService")
public class HmRoleMenuService {

    @Autowired
    private HmRoleMenuRepository hmRoleMenuRepository;

    @Autowired
    private HmRoleMenuMapper hmRoleMenuMapper;


    public void insertToDb(Long roleId,List<Long> menuIdList){
        menuIdList.forEach(menuId ->{
            HmRoleMenuPO hmRoleMenu = new HmRoleMenuPO();
            hmRoleMenu.setRoleId(roleId).setMenuId(menuId);
            hmRoleMenuRepository.save(hmRoleMenu);
        });
    }

    public void deleteByRoleId(Long roleId){
        QueryWrapper<HmRoleMenuPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmRoleMenuFields.ROLE_ID,roleId);
        hmRoleMenuMapper.delete(queryWrapper);
    }

    public List<Long> findByRoleId(Long roleId){
        QueryWrapper<HmRoleMenuPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(HmRoleMenuFields.ROLE_ID,roleId);
        List<HmRoleMenuPO> hmRoleMenuPOList = hmRoleMenuRepository.list(queryWrapper);
        return hmRoleMenuPOList.stream().map(HmRoleMenuPO::getMenuId).collect(Collectors.toList());
    }

}